listview: Expose GtkListItemFactory APIs
authorBenjamin Otte <otte@redhat.com>
Tue, 8 Oct 2019 22:07:07 +0000 (00:07 +0200)
committerMatthias Clasen <mclasen@redhat.com>
Sat, 30 May 2020 23:26:45 +0000 (19:26 -0400)
Due to the many different ways to set factories, it makes sense to
expose them as custom objects.

This makes the actual APIs for the list widgets simpler, because they
can just have a regular "factory" property.

As a convenience function, gtk_list_view_new_with_factory() was added
to make this whole approach easy to use from C.

19 files changed:
demos/gtk-demo/listview_weather.c
docs/reference/gtk/gtk4-sections.txt
docs/reference/gtk/gtk4.types.in
gtk/gtk.h
gtk/gtkbuilderlistitemfactory.c
gtk/gtkbuilderlistitemfactory.h [new file with mode: 0644]
gtk/gtkbuilderlistitemfactoryprivate.h [deleted file]
gtk/gtkfunctionslistitemfactory.c
gtk/gtkfunctionslistitemfactory.h [new file with mode: 0644]
gtk/gtkfunctionslistitemfactoryprivate.h [deleted file]
gtk/gtklistitem.h
gtk/gtklistitemfactory.h [new file with mode: 0644]
gtk/gtklistitemfactoryprivate.h
gtk/gtklistview.c
gtk/gtklistview.h
gtk/gtktypes.h
gtk/meson.build
tests/testlistview-animating.c
tests/testlistview.c

index 6358e9e2c92961157ab370c0da6e1f0c1351d0ba..48f725b140ddb5461cb4d8da9f980f53ece4d235 100644 (file)
@@ -291,13 +291,12 @@ do_listview_weather (GtkWidget *do_widget)
       sw = gtk_scrolled_window_new (NULL, NULL);
       gtk_window_set_child (GTK_WINDOW (window), sw);
     
-      listview = gtk_list_view_new ();
+      listview = gtk_list_view_new_with_factory (
+        gtk_functions_list_item_factory_new (setup_widget,
+                                             bind_widget,
+                                             NULL, NULL));
       gtk_orientable_set_orientation (GTK_ORIENTABLE (listview), GTK_ORIENTATION_HORIZONTAL);
       gtk_list_view_set_show_separators (GTK_LIST_VIEW (listview), TRUE);
-      gtk_list_view_set_functions (GTK_LIST_VIEW (listview),
-                                   setup_widget,
-                                   bind_widget,
-                                   NULL, NULL);
       model = create_weather_model ();
       selection = G_LIST_MODEL (gtk_no_selection_new (model));
       gtk_list_view_set_model (GTK_LIST_VIEW (listview), selection);
index da60b34aafe1d97f72b6fad94d316c1ebb925847..5fbfb9c2a34aa779be4b075af652bdb4923b6cea 100644 (file)
@@ -429,11 +429,34 @@ GTK_TYPE_LIST_ITEM_FACTORY
 gtk_list_item_factory_get_type
 </SECTION>
 
+<SECTION>
+<FILE>gtkbuilderlistitemfactory</FILE>
+<TITLE>GtkBuilderListItemFactory</TITLE>
+GtkBuilderListItemFactory
+gtk_builder_list_item_factory_new_from_bytes
+gtk_builder_list_item_factory_new_from_resource
+gtk_builder_list_item_factory_get_bytes
+gtk_builder_list_item_factory_get_resource
+gtk_builder_list_item_factory_get_scope
+<SUBSECTION Standard>
+GTK_BUILDER_LIST_ITEM_FACTORY
+GTK_BUILDER_LIST_ITEM_FACTORY_CLASS
+GTK_BUILDER_LIST_ITEM_FACTORY_GET_CLASS
+GTK_IS_BUILDER_LIST_ITEM_FACTORY
+GTK_IS_BUILDER_LIST_ITEM_FACTORY_CLASS
+GTK_TYPE_BUILDER_LIST_ITEM_FACTORY
+<SUBSECTION Private>
+gtk_builder_list_item_factory_get_type
+</SECTION>
+
 <SECTION>
 <FILE>gtklistview</FILE>
 <TITLE>GtkListView</TITLE>
 GtkListView
 gtk_list_view_new
+gtk_list_view_new_with_factory
+gtk_list_view_set_factory
+gtk_list_view_get_factory
 gtk_list_view_set_model
 gtk_list_view_get_model
 gtk_list_view_set_show_separators
index 4054e2912aef913af69d175f5555c4b47ecdc8dd..6fc32f914af18e04b69d1d39754e4e1dc8e00fe0 100644 (file)
@@ -20,10 +20,11 @@ gtk_assistant_page_get_type
 gtk_bin_layout_get_type
 gtk_box_get_type
 gtk_box_layout_get_type
+gtk_buildable_get_type
 gtk_builder_cscope_get_type
 gtk_builder_get_type
+gtk_builder_list_item_factory_get_type
 gtk_builder_scope_get_type
-gtk_buildable_get_type
 gtk_button_get_type
 gtk_calendar_get_type
 gtk_cell_area_get_type
index 759ea0eac90bef360bd765751644901048456161..0ad9b5c4126f02fef0f4d520976d96f8fd8b224e 100644 (file)
--- a/gtk/gtk.h
+++ b/gtk/gtk.h
@@ -52,6 +52,7 @@
 #include <gtk/gtkbox.h>
 #include <gtk/gtkbuildable.h>
 #include <gtk/gtkbuilder.h>
+#include <gtk/gtkbuilderlistitemfactory.h>
 #include <gtk/gtkbuilderscope.h>
 #include <gtk/gtkbutton.h>
 #include <gtk/gtkcalendar.h>
 #include <gtk/gtkfontchooserdialog.h>
 #include <gtk/gtkfontchooserwidget.h>
 #include <gtk/gtkframe.h>
+#include <gtk/gtkfunctionslistitemfactory.h>
 #include <gtk/gtkgesture.h>
 #include <gtk/gtkgestureclick.h>
 #include <gtk/gtkgesturedrag.h>
 #include <gtk/gtklinkbutton.h>
 #include <gtk/gtklistbox.h>
 #include <gtk/gtklistitem.h>
+#include <gtk/gtklistitemfactory.h>
 #include <gtk/gtkliststore.h>
 #include <gtk/gtklistview.h>
 #include <gtk/gtklockbutton.h>
index eb8bf668da1ed8bcd15f9b47627431aa8202c2d4..756cef8a131a54cd6354d1a71faa45f80df570a2 100644 (file)
 
 #include "config.h"
 
-#include "gtkbuilderlistitemfactoryprivate.h"
+#include "gtkbuilderlistitemfactory.h"
 
 #include "gtkbuilder.h"
+#include "gtklistitemfactoryprivate.h"
 #include "gtklistitemprivate.h"
 
+/**
+ * SECTION:gtkbuilderlistitemfactory
+ * @Tiitle: GtkBuilderListItemFactory
+ * @Short_description: A listitem factory using ui files
+ *
+ * #GtkBuilderListItemFactory is a #GtkListItemFactory that creates
+ * widgets by instantiating #GtkBuilder UI templates. The templates
+ * must be extending #GtkListItem, and typically use #GtkExpressions
+ * to obtain data from the items in the model.
+ *
+ * Example:
+ * |[
+ *   <interface>
+ *     <template class="GtkListItem">
+ *       <property name="child">
+ *         <object class="GtkLabel">
+ *           <property name="xalign">0</property>
+ *           <binding name="label">
+ *             <lookup name="name" type="SettingsKey">
+ *               <lookup name="item">GtkListItem</lookup>
+ *             </lookup>
+ *           </binding>
+ *         </object>
+ *       </property>
+ *     </template>
+ *   </interface>
+ * ]|
+ */
+
 struct _GtkBuilderListItemFactory
 {
   GtkListItemFactory parent_instance;
@@ -96,6 +126,15 @@ gtk_builder_list_item_factory_init (GtkBuilderListItemFactory *self)
 {
 }
 
+/**
+ * gtk_builder_list_item_factory_new_from_bytes:
+ * @bytes: the bytes containing the ui file to instantiate
+ *
+ * Creates s new #GtkBuilderListItemFactory that instantiates widgets
+ * using @bytes as the data to pass to #GtkBuilder.
+ *
+ * Returns: a new #GtkBuilderListItemFactory
+ **/
 GtkListItemFactory *
 gtk_builder_list_item_factory_new_from_bytes (GBytes *bytes)
 {
@@ -110,6 +149,15 @@ gtk_builder_list_item_factory_new_from_bytes (GBytes *bytes)
   return GTK_LIST_ITEM_FACTORY (self);
 }
 
+/**
+ * gtk_builder_list_item_factory_new_from_resource:
+ * @resource_path: valid path to a resource that contains the data
+ *
+ * Creates s new #GtkBuilderListItemFactory that instantiates widgets
+ * using data read from the given @resource_path to pass to #GtkBuilder.
+ *
+ * Returns: a new #GtkBuilderListItemFactory
+ **/
 GtkListItemFactory *
 gtk_builder_list_item_factory_new_from_resource (const char *resource_path)
 {
diff --git a/gtk/gtkbuilderlistitemfactory.h b/gtk/gtkbuilderlistitemfactory.h
new file mode 100644 (file)
index 0000000..f0459e4
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * Copyright © 2019 Benjamin Otte
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Benjamin Otte <otte@gnome.org>
+ */
+
+#ifndef __GTK_BUILDER_LIST_ITEM_FACTORY_H__
+#define __GTK_BUILDER_LIST_ITEM_FACTORY_H__
+
+#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
+#error "Only <gtk/gtk.h> can be included directly."
+#endif
+
+#include <gtk/gtklistitemfactory.h>
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_BUILDER_LIST_ITEM_FACTORY         (gtk_builder_list_item_factory_get_type ())
+#define GTK_BUILDER_LIST_ITEM_FACTORY(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), GTK_TYPE_BUILDER_LIST_ITEM_FACTORY, GtkBuilderListItemFactory))
+#define GTK_BUILDER_LIST_ITEM_FACTORY_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST ((k), GTK_TYPE_BUILDER_LIST_ITEM_FACTORY, GtkBuilderListItemFactoryClass))
+#define GTK_IS_BUILDER_LIST_ITEM_FACTORY(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), GTK_TYPE_BUILDER_LIST_ITEM_FACTORY))
+#define GTK_IS_BUILDER_LIST_ITEM_FACTORY_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), GTK_TYPE_BUILDER_LIST_ITEM_FACTORY))
+#define GTK_BUILDER_LIST_ITEM_FACTORY_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GTK_TYPE_BUILDER_LIST_ITEM_FACTORY, GtkBuilderListItemFactoryClass))
+
+typedef struct _GtkBuilderListItemFactory GtkBuilderListItemFactory;
+typedef struct _GtkBuilderListItemFactoryClass GtkBuilderListItemFactoryClass;
+
+GDK_AVAILABLE_IN_ALL
+GType                   gtk_builder_list_item_factory_get_type          (void) G_GNUC_CONST;
+
+GDK_AVAILABLE_IN_ALL
+GtkListItemFactory *    gtk_builder_list_item_factory_new_from_bytes    (GBytes         *bytes);
+GDK_AVAILABLE_IN_ALL
+GtkListItemFactory *    gtk_builder_list_item_factory_new_from_resource (const char     *resource_path);
+
+
+G_END_DECLS
+
+#endif /* __GTK_BUILDER_LIST_ITEM_FACTORY_H__ */
diff --git a/gtk/gtkbuilderlistitemfactoryprivate.h b/gtk/gtkbuilderlistitemfactoryprivate.h
deleted file mode 100644 (file)
index b2cbf1b..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * Copyright © 2019 Benjamin Otte
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library. If not, see <http://www.gnu.org/licenses/>.
- *
- * Authors: Benjamin Otte <otte@gnome.org>
- */
-
-
-#ifndef __GTK_BUILDER_LIST_ITEM_FACTORY_H__
-#define __GTK_BUILDER_LIST_ITEM_FACTORY_H__
-
-#include "gtklistitemfactoryprivate.h"
-
-G_BEGIN_DECLS
-
-#define GTK_TYPE_BUILDER_LIST_ITEM_FACTORY         (gtk_builder_list_item_factory_get_type ())
-#define GTK_BUILDER_LIST_ITEM_FACTORY(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), GTK_TYPE_BUILDER_LIST_ITEM_FACTORY, GtkBuilderListItemFactory))
-#define GTK_BUILDER_LIST_ITEM_FACTORY_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST ((k), GTK_TYPE_BUILDER_LIST_ITEM_FACTORY, GtkBuilderListItemFactoryClass))
-#define GTK_IS_BUILDER_LIST_ITEM_FACTORY(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), GTK_TYPE_BUILDER_LIST_ITEM_FACTORY))
-#define GTK_IS_BUILDER_LIST_ITEM_FACTORY_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), GTK_TYPE_BUILDER_LIST_ITEM_FACTORY))
-#define GTK_BUILDER_LIST_ITEM_FACTORY_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GTK_TYPE_BUILDER_LIST_ITEM_FACTORY, GtkBuilderListItemFactoryClass))
-
-typedef struct _GtkBuilderListItemFactory GtkBuilderListItemFactory;
-typedef struct _GtkBuilderListItemFactoryClass GtkBuilderListItemFactoryClass;
-
-GType                   gtk_builder_list_item_factory_get_type          (void) G_GNUC_CONST;
-
-GtkListItemFactory *    gtk_builder_list_item_factory_new_from_bytes    (GBytes         *bytes);
-GtkListItemFactory *    gtk_builder_list_item_factory_new_from_resource (const char     *resource_path);
-
-
-G_END_DECLS
-
-#endif /* __GTK_BUILDER_LIST_ITEM_FACTORY_H__ */
index 6841a2a36b5a33c1f4a022ee2375fcb7b84adbdd..6b7c2814b962071eee391b2db7ff7584faa0a8cd 100644 (file)
@@ -19,8 +19,9 @@
 
 #include "config.h"
 
-#include "gtkfunctionslistitemfactoryprivate.h"
+#include "gtkfunctionslistitemfactory.h"
 
+#include "gtklistitemfactoryprivate.h"
 #include "gtklistitemprivate.h"
 
 struct _GtkFunctionsListItemFactory
diff --git a/gtk/gtkfunctionslistitemfactory.h b/gtk/gtkfunctionslistitemfactory.h
new file mode 100644 (file)
index 0000000..116ef52
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * Copyright © 2019 Benjamin Otte
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Benjamin Otte <otte@gnome.org>
+ */
+
+#ifndef __GTK_FUNCTIONS_LIST_ITEM_FACTORY_H__
+#define __GTK_FUNCTIONS_LIST_ITEM_FACTORY_H__
+
+#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
+#error "Only <gtk/gtk.h> can be included directly."
+#endif
+
+#include <gtk/gtklistitemfactory.h>
+#include <gtk/gtklistitem.h>
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_FUNCTIONS_LIST_ITEM_FACTORY         (gtk_functions_list_item_factory_get_type ())
+#define GTK_FUNCTIONS_LIST_ITEM_FACTORY(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), GTK_TYPE_FUNCTIONS_LIST_ITEM_FACTORY, GtkFunctionsListItemFactory))
+#define GTK_FUNCTIONS_LIST_ITEM_FACTORY_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST ((k), GTK_TYPE_FUNCTIONS_LIST_ITEM_FACTORY, GtkFunctionsListItemFactoryClass))
+#define GTK_IS_FUNCTIONS_LIST_ITEM_FACTORY(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), GTK_TYPE_FUNCTIONS_LIST_ITEM_FACTORY))
+#define GTK_IS_FUNCTIONS_LIST_ITEM_FACTORY_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), GTK_TYPE_FUNCTIONS_LIST_ITEM_FACTORY))
+#define GTK_FUNCTIONS_LIST_ITEM_FACTORY_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GTK_TYPE_FUNCTIONS_LIST_ITEM_FACTORY, GtkFunctionsListItemFactoryClass))
+
+typedef struct _GtkFunctionsListItemFactory GtkFunctionsListItemFactory;
+typedef struct _GtkFunctionsListItemFactoryClass GtkFunctionsListItemFactoryClass;
+
+/**
+ * GtkListItemSetupFunc:
+ * @item: the #GtkListItem to set up
+ * @user_data: (closure): user data
+ *
+ * Called whenever a new list item needs to be setup for managing a row in
+ * the list.
+ *
+ * At this point, the list item is not bound yet, so gtk_list_item_get_item()
+ * will return %NULL.
+ * The list item will later be bound to an item via the #GtkListItemBindFunc.
+ */
+typedef void (* GtkListItemSetupFunc) (GtkListItem *item, gpointer user_data);
+
+/**
+ * GtkListItemBindFunc:
+ * @item: the #GtkListItem to bind
+ * @user_data: (closure): user data
+ *
+ * Binds a#GtkListItem previously set up via a #GtkListItemSetupFunc to
+ * an @item.
+ *
+ * Rebinding a @item to different @items is supported as well as
+ * unbinding it by setting @item to %NULL.
+ */
+typedef void (* GtkListItemBindFunc) (GtkListItem *item,
+                                      gpointer   user_data);
+
+GDK_AVAILABLE_IN_ALL
+GType                   gtk_functions_list_item_factory_get_type (void) G_GNUC_CONST;
+
+GDK_AVAILABLE_IN_ALL
+GtkListItemFactory *    gtk_functions_list_item_factory_new     (GtkListItemSetupFunc    setup_func,
+                                                                 GtkListItemBindFunc     bind_func,
+                                                                 gpointer                user_data,
+                                                                 GDestroyNotify          user_destroy);
+
+
+G_END_DECLS
+
+#endif /* __GTK_FUNCTIONS_LIST_ITEM_FACTORY_H__ */
diff --git a/gtk/gtkfunctionslistitemfactoryprivate.h b/gtk/gtkfunctionslistitemfactoryprivate.h
deleted file mode 100644 (file)
index aeac050..0000000
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
- * Copyright © 2019 Benjamin Otte
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library. If not, see <http://www.gnu.org/licenses/>.
- *
- * Authors: Benjamin Otte <otte@gnome.org>
- */
-
-
-#ifndef __GTK_FUNCTIONS_LIST_ITEM_FACTORY_H__
-#define __GTK_FUNCTIONS_LIST_ITEM_FACTORY_H__
-
-#include "gtklistitemfactoryprivate.h"
-
-G_BEGIN_DECLS
-
-#define GTK_TYPE_FUNCTIONS_LIST_ITEM_FACTORY         (gtk_functions_list_item_factory_get_type ())
-#define GTK_FUNCTIONS_LIST_ITEM_FACTORY(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), GTK_TYPE_FUNCTIONS_LIST_ITEM_FACTORY, GtkFunctionsListItemFactory))
-#define GTK_FUNCTIONS_LIST_ITEM_FACTORY_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST ((k), GTK_TYPE_FUNCTIONS_LIST_ITEM_FACTORY, GtkFunctionsListItemFactoryClass))
-#define GTK_IS_FUNCTIONS_LIST_ITEM_FACTORY(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), GTK_TYPE_FUNCTIONS_LIST_ITEM_FACTORY))
-#define GTK_IS_FUNCTIONS_LIST_ITEM_FACTORY_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), GTK_TYPE_FUNCTIONS_LIST_ITEM_FACTORY))
-#define GTK_FUNCTIONS_LIST_ITEM_FACTORY_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GTK_TYPE_FUNCTIONS_LIST_ITEM_FACTORY, GtkFunctionsListItemFactoryClass))
-
-typedef struct _GtkFunctionsListItemFactory GtkFunctionsListItemFactory;
-typedef struct _GtkFunctionsListItemFactoryClass GtkFunctionsListItemFactoryClass;
-
-GType                   gtk_functions_list_item_factory_get_type (void) G_GNUC_CONST;
-
-GtkListItemFactory *    gtk_functions_list_item_factory_new     (GtkListItemSetupFunc    setup_func,
-                                                                 GtkListItemBindFunc     bind_func,
-                                                                 gpointer                user_data,
-                                                                 GDestroyNotify          user_destroy);
-
-
-G_END_DECLS
-
-#endif /* __GTK_FUNCTIONS_LIST_ITEM_FACTORY_H__ */
index 65d0dcdb6807324140cd090daac835d8907fd95c..fe6269ac784f477b203115e50648879f9a93fec4 100644 (file)
@@ -41,34 +41,6 @@ typedef struct _GtkListItemClass GtkListItemClass;
 GDK_AVAILABLE_IN_ALL
 GType           gtk_list_item_get_type                          (void) G_GNUC_CONST;
 
-/**
- * GtkListItemSetupFunc:
- * @item: the #GtkListItem to set up
- * @user_data: (closure): user data
- *
- * Called whenever a new list item needs to be setup for managing a row in
- * the list.
- *
- * At this point, the list item is not bound yet, so gtk_list_item_get_item()
- * will return %NULL.
- * The list item will later be bound to an item via the #GtkListItemBindFunc.
- */
-typedef void (* GtkListItemSetupFunc) (GtkListItem *item, gpointer user_data);
-
-/**
- * GtkListItemBindFunc:
- * @item: the #GtkListItem to bind
- * @user_data: (closure): user data
- *
- * Binds a#GtkListItem previously set up via a #GtkListItemSetupFunc to
- * an @item.
- *
- * Rebinding a @item to different @items is supported as well as
- * unbinding it by setting @item to %NULL.
- */
-typedef void (* GtkListItemBindFunc) (GtkListItem *item,
-                                      gpointer   user_data);
-
 GDK_AVAILABLE_IN_ALL
 gpointer        gtk_list_item_get_item                          (GtkListItem            *self);
 GDK_AVAILABLE_IN_ALL
diff --git a/gtk/gtklistitemfactory.h b/gtk/gtklistitemfactory.h
new file mode 100644 (file)
index 0000000..629beb6
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * Copyright © 2019 Benjamin Otte
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Benjamin Otte <otte@gnome.org>
+ */
+
+#ifndef __GTK_LIST_ITEM_FACTORY_H__
+#define __GTK_LIST_ITEM_FACTORY_H__
+
+#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
+#error "Only <gtk/gtk.h> can be included directly."
+#endif
+
+typedef struct _GtkListItemFactoryClass GtkListItemFactoryClass;
+
+#include <gdk/gdk.h>
+#include <gtk/gtktypes.h>
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_LIST_ITEM_FACTORY         (gtk_list_item_factory_get_type ())
+#define GTK_LIST_ITEM_FACTORY(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), GTK_TYPE_LIST_ITEM_FACTORY, GtkListItemFactory))
+#define GTK_LIST_ITEM_FACTORY_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST ((k), GTK_TYPE_LIST_ITEM_FACTORY, GtkListItemFactoryClass))
+#define GTK_IS_LIST_ITEM_FACTORY(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), GTK_TYPE_LIST_ITEM_FACTORY))
+#define GTK_IS_LIST_ITEM_FACTORY_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), GTK_TYPE_LIST_ITEM_FACTORY))
+#define GTK_LIST_ITEM_FACTORY_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GTK_TYPE_LIST_ITEM_FACTORY, GtkListItemFactoryClass))
+
+
+GDK_AVAILABLE_IN_ALL
+GType        gtk_list_item_factory_get_type       (void) G_GNUC_CONST;
+
+
+G_END_DECLS
+
+#endif /* __GTK_LIST_ITEM_FACTORY_H__ */
index dc32429dd21f1e30d382e6a3c186ac8558642121..d948db5ede768859ff6c0135627e56c4885bd53c 100644 (file)
  */
 
 
-#ifndef __GTK_LIST_ITEM_FACTORY_H__
-#define __GTK_LIST_ITEM_FACTORY_H__
+#ifndef __GTK_LIST_ITEM_FACTORY_PRIVATE_H__
+#define __GTK_LIST_ITEM_FACTORY_PRIVATE_H__
 
 #include <gtk/gtklistitem.h>
+#include <gtk/gtklistitemfactory.h>
 #include <gtk/gtklistview.h>
 
 G_BEGIN_DECLS
 
-#define GTK_TYPE_LIST_ITEM_FACTORY         (gtk_list_item_factory_get_type ())
-#define GTK_LIST_ITEM_FACTORY(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), GTK_TYPE_LIST_ITEM_FACTORY, GtkListItemFactory))
-#define GTK_LIST_ITEM_FACTORY_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST ((k), GTK_TYPE_LIST_ITEM_FACTORY, GtkListItemFactoryClass))
-#define GTK_IS_LIST_ITEM_FACTORY(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), GTK_TYPE_LIST_ITEM_FACTORY))
-#define GTK_IS_LIST_ITEM_FACTORY_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), GTK_TYPE_LIST_ITEM_FACTORY))
-#define GTK_LIST_ITEM_FACTORY_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GTK_TYPE_LIST_ITEM_FACTORY, GtkListItemFactoryClass))
-
-typedef struct _GtkListItemFactory GtkListItemFactory;
-typedef struct _GtkListItemFactoryClass GtkListItemFactoryClass;
-
 struct _GtkListItemFactory
 {
   GObject parent_instance;
@@ -74,8 +65,6 @@ struct _GtkListItemFactoryClass
                                                                  GtkListItem            *list_item);
 };
 
-GType                   gtk_list_item_factory_get_type          (void) G_GNUC_CONST;
-
 void                    gtk_list_item_factory_setup             (GtkListItemFactory     *self,
                                                                  GtkListItem            *list_item);
 void                    gtk_list_item_factory_teardown          (GtkListItemFactory     *self,
@@ -101,4 +90,4 @@ void                    gtk_list_item_factory_unbind            (GtkListItemFact
 
 G_END_DECLS
 
-#endif /* __GTK_LIST_ITEM_FACTORY_H__ */
+#endif /* __GTK_LIST_ITEM_FACTORY_PRIVATE_H__ */
index 3d9b33c03338f35723fde3b9cb4d85afce15d085..27bac8e4840ae157278cb29d754af5f784b254f5 100644 (file)
@@ -22,8 +22,6 @@
 #include "gtklistview.h"
 
 #include "gtkadjustment.h"
-#include "gtkbuilderlistitemfactoryprivate.h"
-#include "gtkfunctionslistitemfactoryprivate.h"
 #include "gtkintl.h"
 #include "gtklistitemmanagerprivate.h"
 #include "gtkorientableprivate.h"
@@ -91,6 +89,7 @@ struct _ListRowAugment
 enum
 {
   PROP_0,
+  PROP_FACTORY,
   PROP_HADJUSTMENT,
   PROP_HSCROLL_POLICY,
   PROP_MODEL,
@@ -636,6 +635,10 @@ gtk_list_view_get_property (GObject    *object,
 
   switch (property_id)
     {
+    case PROP_FACTORY:
+      g_value_set_object (value, gtk_list_item_manager_get_factory (self->item_manager));
+      break;
+
     case PROP_HADJUSTMENT:
       g_value_set_object (value, self->adjustment[GTK_ORIENTATION_HORIZONTAL]);
       break;
@@ -721,6 +724,10 @@ gtk_list_view_set_property (GObject      *object,
 
   switch (property_id)
     {
+    case PROP_FACTORY:
+      gtk_list_view_set_factory (self, g_value_get_object (value));
+      break;
+
     case PROP_HADJUSTMENT:
       gtk_list_view_set_adjustment (self, GTK_ORIENTATION_HORIZONTAL, g_value_get_object (value));
       break;
@@ -912,6 +919,18 @@ gtk_list_view_class_init (GtkListViewClass *klass)
       g_param_spec_override ("vscroll-policy",
                              g_object_interface_find_property (iface, "vscroll-policy"));
 
+  /**
+   * GtkListView:factory:
+   *
+   * Factory for populating list items
+   */
+  properties[PROP_FACTORY] =
+    g_param_spec_object ("factory",
+                         P_("Factory"),
+                         P_("Factory for populating list items"),
+                         GTK_TYPE_LIST_ITEM_FACTORY,
+                         G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
+
   /**
    * GtkListView:model:
    *
@@ -1009,8 +1028,9 @@ gtk_list_view_init (GtkListView *self)
  *
  * Creates a new empty #GtkListView.
  *
- * You most likely want to call gtk_list_view_set_model() to set
- * a model and then set up a way to map its items to widgets next.
+ * You most likely want to call gtk_list_view_set_factory() to
+ * set up a way to map its items to widgets and gtk_list_view_set_model()
+ * to set a model to provide items next.
  *
  * Returns: a new #GtkListView
  **/
@@ -1020,6 +1040,41 @@ gtk_list_view_new (void)
   return g_object_new (GTK_TYPE_LIST_VIEW, NULL);
 }
 
+/**
+ * gtk_list_view_new_with_factory:
+ * @factory: (transfer full): The factory to populate items with
+ *
+ * Creates a new #GtkListView that uses the given @factory for
+ * mapping items to widgets.
+ *
+ * You most likely want to call gtk_list_view_set_model() to set
+ * a model next.
+ *
+ * The function takes ownership of the
+ * argument, so you can write code like
+ * ```
+ *   list_view = gtk_list_view_new_with_factory (
+ *     gtk_builder_list_item_factory_newfrom_resource ("/resource.ui"));
+ * ```
+ *
+ * Returns: a new #GtkListView using the given @factory
+ **/
+GtkWidget *
+gtk_list_view_new_with_factory (GtkListItemFactory *factory)
+{
+  GtkWidget *result;
+
+  g_return_val_if_fail (GTK_IS_LIST_ITEM_FACTORY (factory), NULL);
+
+  result = g_object_new (GTK_TYPE_LIST_VIEW,
+                         "factory", factory,
+                         NULL);
+
+  g_object_unref (factory);
+
+  return result;
+}
+
 /**
  * gtk_list_view_get_model:
  * @self: a #GtkListView
@@ -1082,50 +1137,42 @@ gtk_list_view_set_model (GtkListView *self,
   g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_MODEL]);
 }
 
-void
-gtk_list_view_set_functions (GtkListView          *self,
-                             GtkListItemSetupFunc  setup_func,
-                             GtkListItemBindFunc   bind_func,
-                             gpointer              user_data,
-                             GDestroyNotify        user_destroy)
+/**
+ * gtk_list_view_get_factory:
+ * @self: a #GtkListView
+ *
+ * Gets the factory that's currently used to populate list items.
+ *
+ * Returns: (nullable) (transfer none): The factory in use
+ **/
+GtkListItemFactory *
+gtk_list_view_get_factory (GtkListView *self)
 {
-  GtkListItemFactory *factory;
-
-  g_return_if_fail (GTK_IS_LIST_VIEW (self));
-  g_return_if_fail (setup_func || bind_func);
-  g_return_if_fail (user_data != NULL || user_destroy == NULL);
+  g_return_val_if_fail (GTK_IS_LIST_VIEW (self), NULL);
 
-  factory = gtk_functions_list_item_factory_new (setup_func, bind_func, user_data, user_destroy);
-  gtk_list_item_manager_set_factory (self->item_manager, factory);
-  g_object_unref (factory);
+  return gtk_list_item_manager_get_factory (self->item_manager);
 }
 
+/**
+ * gtk_list_view_set_factory:
+ * @self: a #GtkListView
+ * @factory: (allow-none) (transfer none): the factory to use or %NULL for none
+ *
+ * Sets the #GtkListItemFactory to use for populating list items.
+ **/
 void
-gtk_list_view_set_factory_from_bytes (GtkListView *self,
-                                      GBytes      *bytes)
+gtk_list_view_set_factory (GtkListView        *self,
+                           GtkListItemFactory *factory)
 {
-  GtkListItemFactory *factory;
-
   g_return_if_fail (GTK_IS_LIST_VIEW (self));
-  g_return_if_fail (bytes != NULL);
+  g_return_if_fail (factory == NULL || GTK_LIST_ITEM_FACTORY (factory));
 
-  factory = gtk_builder_list_item_factory_new_from_bytes (bytes);
-  gtk_list_item_manager_set_factory (self->item_manager, factory);
-  g_object_unref (factory);
-}
-
-void
-gtk_list_view_set_factory_from_resource (GtkListView *self,
-                                         const char  *resource_path)
-{
-  GtkListItemFactory *factory;
-
-  g_return_if_fail (GTK_IS_LIST_VIEW (self));
-  g_return_if_fail (resource_path != NULL);
+  if (factory == gtk_list_item_manager_get_factory (self->item_manager))
+    return;
 
-  factory = gtk_builder_list_item_factory_new_from_resource (resource_path);
   gtk_list_item_manager_set_factory (self->item_manager, factory);
-  g_object_unref (factory);
+
+  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_FACTORY]);
 }
 
 /**
index f953c360ab41388e70c3e8c03369b10919d6fddf..205cf8e9aa6bc1ba9fad9dc2781c06f52f4f5d1d 100644 (file)
@@ -36,6 +36,8 @@ G_DECLARE_FINAL_TYPE (GtkListView, gtk_list_view, GTK, LIST_VIEW, GtkWidget)
 
 GDK_AVAILABLE_IN_ALL
 GtkWidget *     gtk_list_view_new                               (void);
+GDK_AVAILABLE_IN_ALL
+GtkWidget *     gtk_list_view_new_with_factory                  (GtkListItemFactory     *factory);
 
 GDK_AVAILABLE_IN_ALL
 GListModel *    gtk_list_view_get_model                         (GtkListView            *self);
@@ -43,17 +45,11 @@ GDK_AVAILABLE_IN_ALL
 void            gtk_list_view_set_model                         (GtkListView            *self,
                                                                  GListModel             *model);
 GDK_AVAILABLE_IN_ALL
-void            gtk_list_view_set_functions                     (GtkListView            *self,
-                                                                 GtkListItemSetupFunc    setup_func,
-                                                                 GtkListItemBindFunc     bind_func,
-                                                                 gpointer                user_data,
-                                                                 GDestroyNotify          user_destroy);
-GDK_AVAILABLE_IN_ALL
-void            gtk_list_view_set_factory_from_bytes            (GtkListView            *self,
-                                                                 GBytes                 *bytes);
+void            gtk_list_view_set_factory                       (GtkListView            *self,
+                                                                 GtkListItemFactory     *factory);
 GDK_AVAILABLE_IN_ALL
-void            gtk_list_view_set_factory_from_resource         (GtkListView            *self,
-                                                                 const char             *resource_path);
+GtkListItemFactory *
+                gtk_list_view_get_factory                       (GtkListView            *self);
 
 GDK_AVAILABLE_IN_ALL
 void            gtk_list_view_set_show_separators               (GtkListView            *self,
index cc8e3ef7fd31b8cbcdcd705f6342b921db6c227d..774568a1d62a4892225d569e0310de240f5813e0 100644 (file)
@@ -41,6 +41,7 @@ typedef struct _GtkCssStyleChange      GtkCssStyleChange;
 typedef struct _GtkEventController     GtkEventController;
 typedef struct _GtkGesture             GtkGesture;
 typedef struct _GtkLayoutManager       GtkLayoutManager;
+typedef struct _GtkListItemFactory     GtkListItemFactory;
 typedef struct _GtkNative              GtkNative;
 typedef struct _GtkRequisition        GtkRequisition;
 typedef struct _GtkRoot               GtkRoot;
index 5a3f2d1543dea18617ebc70e9d44b578f9b82dfa..d6b2135b14139288853a46a663a3d56dfe5494c4 100644 (file)
@@ -438,6 +438,7 @@ gtk_public_headers = files([
   'gtkboxlayout.h',
   'gtkbuildable.h',
   'gtkbuilder.h',
+  'gtkbuilderlistitemfactory.h',
   'gtkbuilderscope.h',
   'gtkbutton.h',
   'gtkcalendar.h',
@@ -513,6 +514,7 @@ gtk_public_headers = files([
   'gtkfontchooserdialog.h',
   'gtkfontchooserwidget.h',
   'gtkframe.h',
+  'gtkfunctionslistitemfactory.h',
   'gtkgesture.h',
   'gtkgesturedrag.h',
   'gtkgesturelongpress.h',
@@ -543,6 +545,7 @@ gtk_public_headers = files([
   'gtklinkbutton.h',
   'gtklistbox.h',
   'gtklistitem.h',
+  'gtklistitemfactory.h',
   'gtkliststore.h',
   'gtklistview.h',
   'gtklockbutton.h',
index 3a0b08dfab631b22cf497169e9fdc4a57c5ffef5..2d68d6b160c264f35a5b406cfb9bd25f23a9b722 100644 (file)
@@ -145,11 +145,10 @@ main (int   argc,
   gtk_widget_set_vexpand (sw, TRUE);
   gtk_box_append (GTK_BOX (vbox), sw);
 
-  listview = gtk_list_view_new ();
-  gtk_list_view_set_functions (GTK_LIST_VIEW (listview),
-                               setup_list_item,
-                               bind_list_item,
-                               NULL, NULL);
+  listview = gtk_list_view_new_with_factory (
+    gtk_functions_list_item_factory_new (setup_list_item,
+                                         bind_list_item,
+                                         NULL, NULL));
   gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), listview);
 
   vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 4);
index 39630e4b83cefae0cfe4ae2cce8af1f9163523ed..0234b9bdc54d6f4b516818b0c5aaedde524af7db 100644 (file)
@@ -566,6 +566,7 @@ update_statusbar (GtkStatusbar *statusbar)
       g_string_append_printf (string, " (%u directories remaining)", active + g_slist_length (pending));
       result = G_SOURCE_CONTINUE;
     }
+      result = G_SOURCE_CONTINUE;
 
   gtk_statusbar_push (statusbar, 0, string->str);
   g_free (string->str);
@@ -627,11 +628,10 @@ main (int argc, char *argv[])
   gtk_search_entry_set_key_capture_widget (GTK_SEARCH_ENTRY (search_entry), sw);
   gtk_box_append (GTK_BOX (vbox), sw);
 
-  listview = gtk_list_view_new ();
-  gtk_list_view_set_functions (GTK_LIST_VIEW (listview),
-                               setup_widget,
-                               NULL,
-                               NULL, NULL);
+  listview = gtk_list_view_new_with_factory (
+    gtk_functions_list_item_factory_new (setup_widget,
+                                         NULL,
+                                         NULL, NULL));
   gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), listview);
 
   if (argc > 1)